
#include "ros/ros.h"
#include <string>
#include <geometry_msgs/Twist.h>
#include <iostream>
#include <iomanip>
#include "std_msgs/String.h"
using std::cout;
using std::endl;
using std::string;

ros::Publisher uc0Command_pub;

/**
 * This tutorial demonstrates simple receipt of messages over the ROS system.
 */
void TeleopCallback(const geometry_msgs::Twist::ConstPtr& msg)
{
  //ROS_INFO("I heard: [%s]", msg->data.c_str());
  geometry_msgs::Twist vel = *msg;
  string str;
  if(vel.angular.z < 0) str = "AL120#"; else
  if(vel.angular.z > 0) str = "AR120#"; else 
  if(vel.linear.x < 0)  str = "AF120#"; else
  if(vel.linear.x > 0)  str = "AB120#"; else
  str = "AR0#";

  std_msgs::String outmsg;
  outmsg.data = str;

  ROS_INFO("Robot_direction: %s", outmsg.data.c_str());

  uc0Command_pub.publish(outmsg);
}

int main(int argc, char **argv)
{
  /**
   * The ros::init() function needs to see argc and argv so that it can perform
   * any ROS arguments and name remapping that were provided at the command line. For programmatic
   * remappings you can use a different version of init() which takes remappings
   * directly, but for most command-line programs, passing argc and argv is the easiest
   * way to do it.  The third argument to init() is the name of the node.
   *
   * You must call one of the versions of ros::init() before using any other
   * part of the ROS system.
   */
  ros::init(argc, argv, "Arduino_Teleop_translate");

  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   */
  ros::NodeHandle n;

  uc0Command_pub = n.advertise<std_msgs::String>("uc0Command", 1);

  ros::Rate loop_rate(1);

  ros::Subscriber sub = n.subscribe("cmd_vel", 1, TeleopCallback);

  /**
   * ros::spin() will enter a loop, pumping callbacks.  With this version, all
   * callbacks will be called from within this thread (the main one).  ros::spin()
   * will exit when Ctrl-C is pressed, or the node is shutdown by the master.
   */
  ros::spin();

  return 0;
}

